import { debug } from 'util';
const url = require('url');
const http = require('http');

export default function start(){
  const server = http.createServer((req, res) => {
    let pathname = url.parse(req.url).pathname;
    routes(pathname,req,res)
  });
  server.on('clientError', (err, socket) => {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
  });
  server.listen(8000);
}

let routeConfig = [];

function routes(path,req,res){
  routeConfig.forEach(item=>{
    if(item.path == path){
      item.call(req,res);
    }
  })
}

export function api(path,call){
  routeConfig.push({
    path:path,
    call:call
  })
}
